﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.SQS;
using Amazon.SQS.Model;
using DevToolBox.Cloud.Common;

namespace DevToolBox.Cloud.Amazon
{
    /// <summary>
    /// Class to encapsulate the operations of an Amazon SQS
    /// </summary>
    public class AmazonSQSOperations: IManageable
    {
        AmazonSQSConfig config = null;
        AmazonSQS client = null;

        /// <summary>
        /// Constructor to build internal objects
        /// </summary>
        /// <param name="serviceUrl"></param>
        public AmazonSQSOperations(string serviceUrl)
        {
            config = new AmazonSQSConfig();
            config.UseSecureStringForAwsSecretKey = false;
            config.ServiceURL = serviceUrl;
            client = AWSClientFactory.CreateAmazonSQSClient(config);
        }

        /// <summary>
        /// Creates a new Amazon SQS queue
        /// </summary>
        /// <param name="configObject">Queue Name</param>
        public void Create(object configObject)
        {
            string readingQueueName = Convert.ToString(configObject);
            var createRequest = new CreateQueueRequest();
            createRequest.QueueName = readingQueueName;
            createRequest.DefaultVisibilityTimeout = 1800; //30 minutes
            CreateQueueResponse createResponse = null;
            createResponse = client.CreateQueue(createRequest);
        }

        /// <summary>
        /// Deletes an Amazon SQS queue
        /// </summary>
        /// <param name="configObject">Queue Name</param>
        public void Delete(string key)
        {
            var deleteRequest = new DeleteQueueRequest();
            deleteRequest.QueueUrl = key;
            client.DeleteQueue(deleteRequest);
        }

        /// <summary>
        /// Get the name (if exists) of a queue
        /// </summary>
        /// <param name="key">Unique queue identifier</param>
        /// <returns>Queue Url (if exists)</returns>
        public string GetStatus(string queueName)
        {
            var listRequest = new ListQueuesRequest();
            listRequest.QueueNamePrefix = queueName;
            var listResponse = client.ListQueues(listRequest);
            if (listRequest != null && listResponse.ListQueuesResult != null
                && listResponse.ListQueuesResult.QueueUrl != null)
            {
                if (listResponse.ListQueuesResult.QueueUrl.Count == 1)
                {
                    return listResponse.ListQueuesResult.QueueUrl[0];
                }
                else
                {
                    //Multiple queues match the pattern
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < listResponse.ListQueuesResult.QueueUrl.Count; i++)
                    {
                        sb.AppendLine(listResponse.ListQueuesResult.QueueUrl[i]);
                    }
                    return sb.ToString();
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Frees unmanaged resources
        /// </summary>
        public void Dispose()
        {
            if (client != null)
            {
                client.Dispose();
            }
        }

        public void Update(object configObject)
        {
            //This operation is not needed yet
            throw new NotImplementedException();
        }
    }
}
